package org.deri.vor.mobilesensing;


import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.deri.vor.R;
import org.deri.vor.mobilesensing.mobilesensor.LocationPushService;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import android.view.View;
import android.view.View.OnClickListener;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.widget.Button;
import android.widget.ImageView;

public class MobileSensing extends Activity {
	Button 	Config,Map;
	Button 	Home, Back, About;
	ImageView vor, nuig, deri;
	
	String 	lastClass 	= "";
	String 	lastPackage = "org.deri.vor";
	String Check	=	"";
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mobilesensing_main);
       
        
		Bundle bundle = getIntent().getExtras();
		
		if (bundle!=null)	{
			Check = Check + bundle.getString("check");
			lastClass = bundle.getString("lastClass");
			
			if (Check.contains("GPS")) {
				GPSStart();
			}else{
				GPSStop();
				sendGeoData();
			}
			
		}

		// Map Button OnClick
		Map = (Button) findViewById(R.id.buttonMSMapView);
		Map.setOnClickListener(MSmapListerner);

		// Setting Button OnClick
		Config = (Button) findViewById(R.id.buttonMSConfig);
		Config.setOnClickListener(MSConfigListerner);

//		LocationManager lm 			= (LocationManager) getSystemService(Context.LOCATION_SERVICE);	
//		Location location 	= lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);	
//		if (location!= null){
//			sendGeoData(location);
//		}
		
		
		share_button();
	}
		
	
	private void share_button(){
		Button button = (Button) findViewById(R.id.btnBack);
		button.setOnClickListener(ms_layout);

		// About Button OnClick
		button = (Button) findViewById(R.id.btnAbout);
		button.setOnClickListener(ms_layout);

		// Home Button OnClick
		button = (Button) findViewById(R.id.btnHome);
		button.setOnClickListener(ms_layout);
		
		ImageView im  = (ImageView) findViewById(R.id.vor_icon);
		im.setClickable(true);
		im.setOnClickListener(ms_layout);

		im = (ImageView) findViewById(R.id.nuig_icon);
		im.setClickable(true);
		im.setOnClickListener(ms_layout);

		im = (ImageView) findViewById(R.id.deri_icon);
		im.setClickable(true);
		im.setOnClickListener(ms_layout);
	}
    
    
    private OnClickListener ms_layout	=	new OnClickListener(){

		@Override
		public void onClick(View v) {
			switch (v.getId()){
			case R.id.btnAbout  : { jump(72); break;}
			case R.id.btnHome 	: { jump(2); break;}
			case R.id.btnBack  	: { jump(2); break;}
			case R.id.vor_icon  : { jump(3); break;}
			case R.id.deri_icon : { jump(19); break;}
			case R.id.nuig_icon : { jump(40); break;}
			}
		}
    };
    
    
    private void jump(int page){
    	Intent i = new Intent();
    	i.putExtra("cl",page);
    	i.setClassName("org.deri.vor", "org.deri.vor.VovolOceanRaceActivity");
		startActivity(i);
		finish();
    }
    
 
	
	
	
	 
   
    OnClickListener	MSmapListerner = new OnClickListener(){
		@Override
		public void onClick(View arg0) {
			Intent i	=	new Intent();
			lastClass	=	"org.deri.vor.mobilesensing.MobileSensingMap";
			i.putExtra("check", Check);
			i.setClassName(lastPackage,lastClass);
			startActivity(i);
			finish();
	}};
	
	
	OnClickListener	MSConfigListerner = new OnClickListener(){
	    	@Override
	    	public void onClick(View arg0) {
	    		Intent	i	=	new Intent();
	    		lastClass	=	"org.deri.vor.mobilesensing.MobileSensingSetting";
	    		i.putExtra("check", Check);
	    		i.setClass(MobileSensing.this, MobileSensingSetting.class);
	    		startActivity(i);
	    		finish();
	    }};
    
	    OnClickListener	MSbackListerner = new OnClickListener(){
    	@Override
    	public void onClick(View arg0) {
    		finish();
    }};
	
	
		
    private void GPSStart(){
    	Intent i = new Intent(MobileSensing.this,LocationPushService.class);
		startService(i);
    }
    
    private void GPSStop(){
    	System.out.println("Stop");
    	Intent i = new Intent(MobileSensing.this,LocationPushService.class);
    	stopService(i);
    }
    
//    private void sendGeoData(Location location){
//		System.out.println("PUSH");
//		String url = "http://lsm.deri.ie/volvorace/oceanrace?api=update&" +
//				"userId=" + Secure.getString(getBaseContext().getContentResolver(), Secure.ANDROID_ID) + 
//				"&lat=" + Double.toString(location.getLatitude()) + 
//				"&long=" + Double.toString(location.getLongitude());
//		
//		try {
//			URL Url	=	new URL(url);
//			Url.openConnection();
//			Url.openStream();
//		
//		} catch (MalformedURLException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//
//			e.printStackTrace();
//		}
//	}
	
	private void sendGeoData(){
		String url = "http://lsm.deri.ie/volvorace/oceanrace?api=update&" +
				"userId=" + Secure.getString(getBaseContext().getContentResolver(), Secure.ANDROID_ID) + 
				"&lat="   + 1000000  + 
				"&long="  + 1000000;
		
		System.out.println(url);
		try {
			URL Url	=	new URL(url);
			Url.openConnection();
			Url.openStream();
		
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}
		
	}
}
